// https://leetcode-cn.com/problems/maximum-product-subarray/submissions/

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size();
        int min_val = 1;
        int max_val = 1;
        int ans = INT_MIN;
        for (int i = 0; i < n; ++i) {
            int min_tmp = min_val * nums[i];
            int max_tmp = max_val * nums[i];
            // min_val = min(min(min_val, min_tmp), max_tmp);
            // max_val = max(max(max_val, max_tmp), min_tmp); 如果还和max_val原来的比较，就不是连续的了
            min_val = min(min(nums[i], min_tmp), max_tmp);
            max_val = max(max(nums[i], max_tmp), min_tmp);
            ans = max(ans, max_val);
        }
        return ans;
    }
};